#include "cmainwindow.h"
#include "ui_cmainwindow.h"
#include "cmorphing.h"

CMainWindow::CMainWindow(QWidget *parent) :
	QMainWindow(parent),
	ui(new Ui::CMainWindow)
{
	ui->setupUi(this);

        this->sphere1 = NULL;
        this->sphere2 = NULL;
}

CMainWindow::~CMainWindow()
{
	delete ui;
}

void CMainWindow::on_actionQuitter_triggered()
{
	this->close();
}

void CMainWindow::on_actionOuvrir_triggered()
{
	QString filters("Maillages (*.obj *.OBJ)");

	QString filename = QFileDialog::getOpenFileName(this, "Choisir le maillage 1...", ":", filters,0, QFileDialog::HideNameFilterDetails);

	if (!filename.isEmpty())
	{
		CMesh * nouveau = new CMesh();
		if (nouveau->LoadOBJ(filename.toStdString().c_str()))
		{
			nouveau->normalizeSize();
                        if(ui->WidgetAff1->nb_objects() > 0)
                            ui->WidgetAff1->remove_object(0);
			ui->WidgetAff1->add_object(nouveau);
                }
                else
                    QMessageBox::information(this, "Erreur de chargement", "Le fichier selectionne n'est pas valide") ;
        }
        else
            QMessageBox::information(this, "Erreur de chargement", "Le fichier selectionne n'existe pas") ;

}

void CMainWindow::on_pushButton_clicked()
{
    if (ui->WidgetAff1->nb_objects() != 0 && ui->WidgetAff2->nb_objects() != 0)
    {
        ui->pushButton->setText("En cours...");
        this->sphere1 = new CMesh ();
        this->sphere2 = new CMesh ();

        clock_t beginTime = clock();

        CMorphing::meshToSphereMesh(ui->WidgetAff1->get_object(0), this->sphere1, ui->doubleSpinBox->value());
        CMorphing::meshToSphereMesh(ui->WidgetAff2->get_object(0), this->sphere2, ui->doubleSpinBox->value());

        clock_t endTime = clock();
        ui->lineEdit->setText(QString::number((endTime - beginTime)/ CLOCKS_PER_SEC)+" sec");
        ui->pushButton->setText("MeshToSphere");

        if(ui->WidgetAff1->nb_objects() > 0)
            ui->WidgetAff1->remove_object(0);
        ui->WidgetAff1->add_object(this->sphere1);

        if(ui->WidgetAff2->nb_objects() > 0)
            ui->WidgetAff2->remove_object(0);
        ui->WidgetAff2->add_object(this->sphere2);

    }
    else
        QMessageBox::information(this, "Operation impossible", "Veuillez ouvrir deux maillages.") ;
}


void CMainWindow::on_actionEnregistrer_triggered()
{
	QString filters("Maillages (*.obj *.OBJ)");

	QString filename = QFileDialog::getOpenFileName(this, "Choisir le maillage...", ":", filters,0, QFileDialog::HideNameFilterDetails);

	if (!filename.isEmpty())
	{
		CMesh * nouveau = new CMesh();
		if (nouveau->LoadOBJ(filename.toStdString().c_str()))
		{
			nouveau->normalizeSize();
			ui->WidgetAff2->add_object(nouveau);
            }
            else
                QMessageBox::information(this, "Erreur de chargement", "Le fichier selectionne n'est pas valide") ;
    }
    else
        QMessageBox::information(this, "Erreur de chargement", "Le fichier selectionne n'existe pas") ;
}

void CMainWindow::on_mode_aff1_clicked()
{
	this->ui->mode_aff2->setChecked(false);
	this->ui->mode_aff3->setChecked(false);
	this->ui->WidgetAff1->set_mode_rendu(GL_TRIANGLES);
	this->ui->WidgetAff2->set_mode_rendu(GL_TRIANGLES);
}

void CMainWindow::on_mode_aff2_clicked()
{
	this->ui->mode_aff1->setChecked(false);
	this->ui->mode_aff3->setChecked(false);
	this->ui->WidgetAff1->set_mode_rendu(GL_LINE_LOOP);
	this->ui->WidgetAff2->set_mode_rendu(GL_LINE_LOOP);
}

void CMainWindow::on_mode_aff3_clicked()
{
	this->ui->mode_aff1->setChecked(false);
	this->ui->mode_aff2->setChecked(false);
	this->ui->WidgetAff1->set_mode_rendu(GL_POINTS);
	this->ui->WidgetAff2->set_mode_rendu(GL_POINTS);
}

void CMainWindow::on_mode_illum_stateChanged(int arg1)
{

}

void CMainWindow::on_mode_illum_clicked(bool checked)
{
	if(checked)
	{
		this->ui->WidgetAff1->enable_lighting();
		this->ui->WidgetAff2->enable_lighting();
	}
	else
	{
		this->ui->WidgetAff1->disable_lighting();
		this->ui->WidgetAff2->disable_lighting();
	}
}

void CMainWindow::on_pushButton_2_clicked()
{
    if (this->sphere1 && this->sphere2)
    {
        this->metamesh = new CMetaMesh(this->sphere1, this->sphere2);

        this->metamesh->merge();

        this->metamesh->buildMorphMesh();

        CMesh * meshTemp = this->metamesh->getMorphMesh();

        if(ui->WidgetAff1->nb_objects() > 0)
            ui->WidgetAff1->objets.clear();
        ui->WidgetAff1->add_object(meshTemp);

    }
    else
        QMessageBox::information(this, "Erreur", "Les spheres n'ont pas ete calculees") ;
}
